﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using ReflectionStudio.Core.Events;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{

    public class JLTNVKDOCData : BaseFunction
    {
        public bool checkVK07 = true;
        SETINTEXDB INTEXDB = new SETINTEXDB();
        public INTEXDBC INTEXDBC;
        public JLTNVKDOCData()
            : base()
        {

        }
        public JLTNVKDOCData(OracleConnection _myConnection, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_myConnection, _proxy, userID)
        {
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
        }
        public JLTNVKDOCData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {

        }
        public DataSet GetData(DataRow dwInput)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();

            try
            {
                DataRow dwTemp;
                DataRow dwVK04=null;
                SResult = INTEXDBC.QueryVKKOPF(dwInput, 1);
                if (SResult.Tables.Count == 0)
                    return SReturn;
                SReturn.Merge(SResult);

                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    dwVK04 = row;
                }
                if (dwVK04 == null)
                    return SReturn;
                //SResult = GetVKKTXT(dwInput);
                //SReturn.Merge(SResult);
                
                dwTemp = INTEXDB.TXTIND.NewRow();
                dwTemp["TXTIMANDANT"] = dwVK04["VKKMANDANT"];
                dwTemp["TXTITXTART"] = "VKT";
                dwTemp["TXTISIND"] = dwVK04["VKKAUFKRS"].ToString() + dwVK04["VKKAUFLFD"].ToString();

                SResult = INTEXDBC.QueryTXTKON(dwTemp, 2);
                dwTemp = setTXT.TXTKON.NewRow();
                SResult = Helper.ConvertTXT(SResult.Tables["TXTKON"], dwTemp);
                SReturn.Merge(SResult);

                dwTemp = INTEXDB.KDSTAMM.NewRow();
                dwTemp["KDMANDANT"] = dwVK04["VKKMANDANT"];
                dwTemp["KDKUNDNR"] = dwVK04["VKKKUNDNR"];
                SResult = INTEXDBC.QueryKDSTAMM(dwTemp, 1);
                SReturn.Merge(SResult);
                dwTemp = INTEXDB.ZABED.NewRow();
                dwTemp["ZABMANDANT"] = dwVK04["VKKMANDANT"];
                dwTemp["ZABNR"] = dwVK04["VKKZAHLBED"];
                SResult = INTEXDBC.QueryZABED(dwTemp, 1);
                SReturn.Merge(SResult);
                dwTemp = INTEXDB.TXTIND.NewRow();
                dwTemp["TXTIMANDANT"] = dwVK04["VKKMANDANT"];
                dwTemp["TXTITXTART"] = "ZAB";
                dwTemp["TXTISIND"] =Helper.Left( dwVK04["VKKZAHLBED"].ToString() +"V                                                                                       ",60);
                SResult = INTEXDBC.QueryTXTKON(dwTemp, 1);
                dwTemp = setTXT.TXTKON.NewRow();
                SResult = Helper.ConvertTXT(SResult.Tables["TXTKON"], dwTemp);
                SReturn.Merge(SResult);
                dwTemp = INTEXDB.TXTIND.NewRow();
                dwTemp["TXTIMANDANT"] = dwVK04["VKKMANDANT"];
                dwTemp["TXTITXTART"] = "ZAB";
                dwTemp["TXTISIND"] = Helper.Left(dwVK04["VKKZAHLBED"].ToString() + "E                                                                                       ", 60);
                SResult = INTEXDBC.QueryTXTKON(dwTemp, 1);
                dwTemp = setTXT.TXTKON.NewRow();
                SResult = Helper.ConvertTXT(SResult.Tables["TXTKON"], dwTemp);
                SReturn.Merge(SResult);

                dwTemp = INTEXDB.VKPOS.NewRow();
                dwTemp["VKPMANDANT"] = dwInput["VKKMANDANT"];
                dwTemp["VKPAUFKRS"] = dwInput["VKKAUFKRS"];
                dwTemp["VKPAUFLFD"] = dwInput["VKKAUFLFD"];
                SResult = INTEXDBC.QueryVKPOS(dwTemp, 3);
                SReturn.Merge(SResult);

                if (SResult.Tables[0].Rows.Count == 0)
                    return SReturn;
                DataRow dwVKP = SResult.Tables[0].Rows[0];
                SResult = INTEXDBC.QueryVAART(dwVKP, 3);
                SReturn.Merge(SResult);
                SResult = INTEXDBC.QueryVAFARBEN(dwVKP, 3);
                SReturn.Merge(SResult);
                SResult = INTEXDBC.QueryVAKDBEZ(dwVKP, 3);
                SReturn.Merge(SResult);                
                SResult = INTEXDBC.QueryTXTKON(dwVKP, 3);
                dwTemp = setTXT.TXTKON.NewRow();
                SResult = Helper.ConvertTXT(SResult.Tables["TXTKON"], dwTemp);
                SReturn.Merge(SResult);      

                dwTemp = INTEXDB.KDSTAMM.NewRow();
                dwTemp["KDMANDANT"] = dwVK04["VKKMANDANT"];
                dwTemp["KDKUNDNR"] = dwVK04["VKKKUNDNR"];
                SReturn.Merge(INTEXDBC.QueryKDSTAMM(dwTemp, 1));
                dwTemp = INTEXDB.KUNDADR.NewRow();
                dwTemp["KDAKUNDNR"] = dwVK04["VKKKUNDNR"];
                SReturn.Merge(INTEXDBC.QueryKUNDADR(dwTemp, 1));
                dwTemp = INTEXDB.KDANSP.NewRow();
                dwTemp["KDSKUNDNR"] = dwVK04["VKKKUNDNR"];
                SReturn.Merge(INTEXDBC.QueryKDANSP(dwTemp, 1));
                dwTemp = INTEXDB.HRSTAMM.NewRow();
                dwTemp["HRMANDANT"] = dwVK04["VKKMANDANT"];
                dwTemp["HRBENID"] = dwVK04["VKKSACHBE"];
                SReturn.Merge(INTEXDBC.QueryHRSTAMM(dwTemp, 1));
                dwTemp["HRBENID"] = dwVK04["VKKNEUBEN"];
                SReturn.Merge(INTEXDBC.QueryHRSTAMM(dwTemp, 1));
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            return SReturn;
        }
        public string Today
        {
            get
            {
                DateTime now = System.DateTime.Now;
                string year = now.Year.ToString();
                string m =Right("00" +now.Month.ToString(),2);
                string day = Right("00" +now.Day.ToString(),2);
                return year + m + day;
            }
        }
        public DataSet LoadQPPLAN()
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
                        DataRow dwTemp;
            try
            {
                dwTemp = INTEXDB.QPPLAN.NewRow();
                dwTemp["QPPMANDANT"] = "00";
                SResult = INTEXDBC.QueryQPPLAN(dwTemp, 2);
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;

        }
        public DataSet GetQPStep(string art,DataRow dwInput=null)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataRow dwTemp;
            DataRow dwQPZUORD;
            try
            {
                if (dwInput == null)
                {
                    dwTemp = INTEXDB.QPZUORD.NewRow();
                    dwTemp["QPZART"] = Left(art, 20);
                    dwTemp["QPZVAR"] = "   ";
                    SResult = INTEXDBC.QueryQPZUORD(dwTemp);
                    SReturn.Merge(SResult);
                    if (!SResult.Tables.Contains("QPZUORD"))
                    {
                        return SReturn;
                    }
                    if (SResult.Tables[0].Rows.Count == 0)
                        return SReturn;
                    dwQPZUORD = SResult.Tables[0].Rows[0];
                }
                else
                {
                    dwQPZUORD = dwInput;
                }
                /*
                dwTemp = INTEXDB.QPPLAN.NewRow();
                dwTemp["QPPPNRKRS"] = dwQPZUORD["QPZPNRKRS"];
                dwTemp["QPPPLFDNR"] = dwQPZUORD["QPZPLFDNR"];
                SResult = INTEXDBC.QueryQPPLAN(dwTemp);
                SReturn.Merge(SResult);
              */
                SResult = INTEXDBC.QueryQPSTEP(dwQPZUORD,2);
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public DataSet GetVKKTXT(DataRow dwInput)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();            
            try
            {
                string KRS = dwInput["VKKAUFKRS"].ToString();
                string LFD = dwInput["VKKAUFLFD"].ToString();
                string VKPAUF = KRS + LFD;
                SResult = GetTXT(VKPAUF);
                string TXTISIND = "";
                string TXTKPOS = "";
                DataRow dwTXTKON = null;
                DataTable tbTXTKON = setTXT.TXTKON.Copy();
       
                int pos = 1;
                Dictionary<string, int> txtID = new Dictionary<string, int>();
                string processed = "";
                foreach (DataRow dwTXT in SResult.Tables["TXTKON"].Rows)
                {
                    TXTISIND = dwTXT["TXTISIND"].ToString();
                    TXTKPOS = dwTXT["TXTKPOS"].ToString();
                    if (!processed.Contains(TXTISIND + ";"))
                    {
                        processed += TXTISIND + ";";
                        dwTXTKON = tbTXTKON.NewRow();
                        tbTXTKON.Rows.Add(dwTXTKON);
                        dwTXTKON["TXTIPIND"] = TXTISIND;
                    }
                    else
                    {
                        var query = from DataRow dw in tbTXTKON.AsEnumerable()
                                    where dw["TXTIPIND"].ToString() == TXTISIND
                                    select dw;
                        foreach (DataRow dw in query)
                        {
                            dwTXTKON = dw;
                        }
                    }

                    if (txtID.ContainsKey(TXTISIND))
                    {
                        pos = txtID[TXTISIND];
                        pos++;
                        txtID[TXTISIND] = pos;
                    }
                    else
                    {
                        pos = 1;
                        txtID.Add(TXTISIND, pos);
                    }
                    TXTKPOS = Right("000" + pos.ToString() + "0", 3);

                    if (tbTXTKON.Columns.Contains("TXT" + TXTKPOS))
                    {
                        dwTXTKON["TXT" + TXTKPOS] = dwTXT["TXTKTEXT"];
                    }

                }
                SReturn.Merge(tbTXTKON);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        } 
        public string GetMemberV9(string company = "DST")
        {
            string memberv9 = "";
            try
            {
                OracleCommand or1;
                or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT HRMANDANT,HRBENID,HRPASSW,HRSPRKZ,HRVNAME,HRNNAME"
                + " FROM HRSTAMM "
                + "WHERE HRBGRP='V9' AND HRBERST=9";
                setResult = Connect2Proxy(or1);
                foreach (DataRow dwTemp in setResult.Tables[0].Rows)
                {
                    memberv9 += dwTemp["HRBENID"].ToString().Trim() + ";";
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return memberv9;
        }
        public string GetAdministration(string company = "DST")
        {
            string Administration = "";
            try
            {
                OracleCommand or1;
                or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT HRMANDANT,HRBENID,HRPASSW,HRSPRKZ,HRVNAME,HRNNAME"
                + " FROM HRSTAMM "
                + "WHERE HRSACHG like '%ADMINISTRATION%' AND HRBERST=9";
                setResult = Connect2Proxy(or1);
                foreach (DataRow dwTemp in setResult.Tables[0].Rows)
                {
                    Administration += dwTemp["HRBENID"].ToString().Trim() + ";";
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Administration;
        }
        public override DataSet LogIn(string MANDANT, string UsingID, System.Security.SecureString sPassword)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult;
            DataTable tableLocal = null;
            OracleCommand or1;
            try
            {
                IntPtr p = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(sPassword);
                string Password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p);
                UsingID = UsingID.Trim().ToUpper();
                Password = Password.Trim().ToUpper();
                DataRow dwTemp = INTEXDB.HRSTAMM.NewRow();
                dwTemp["HRMANDANT"] = MANDANT;
                dwTemp["HRBENID"] = Left(UsingID + "                    ", 8);
                dwTemp["HRDFAX"] =Left(Password+"                    ",20);
                SResult = INTEXDBC.QueryHRSTAMM(dwTemp, 1);
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return SReturn;
        }
    }
}